<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告位置" prop="position">
        <el-select v-model="queryParams.position" placeholder="请选择">
          <el-option
            v-for="item in positionList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入广告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['smy:advertising:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="advertisingList" border>
      <el-table-column label="序号" align="center" prop="id" width="80" />
      <el-table-column label="平台类型" align="center" prop="platformType" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.platformType === 0 ">商城端</span>
          <span v-if="scope.row.platformType === 1 ">采购端</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告图" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="广告名称" align="center" prop="name" />
      <el-table-column label="广告位置" align="center" prop="position" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.position === 1 ">首页海报图</span>
          <span v-if="scope.row.position === 2 ">首页通栏广告</span>
          <span v-if="scope.row.position === 3 ">首页小广告</span>
          <span v-if="scope.row.position === 4 ">成功支付广告</span>
          <span v-if="scope.row.position === 5 ">领券中心</span>
          <span v-if="scope.row.position === 6 ">秒杀活动</span>
          <span v-if="scope.row.position === 7 ">特价活动</span>
          <span v-if="scope.row.position === 8 ">预售活动</span>
          <span v-if="scope.row.position === 9 ">拼团活动</span>
          <span v-if="scope.row.position === 10 ">区域加盟商广告</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="rank" width="80"/>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1 " style="color: red;font-weight: bold">下架</span>
          <span v-if="scope.row.status === 2 ">上架</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['smy:advertising:edit']"
            v-if="scope.row.status===1"
          >修改</el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-close"
            @click="handleChangeStatus(scope.row,'N')"
            v-hasPermi="['smy:advertising:edit']"
            v-if="scope.row.status===2"
          >下架</el-button>
          <el-button
            size="small"
            type="success"
            icon="el-icon-check"
            @click="handleChangeStatus(scope.row,'Y')"
            v-hasPermi="['smy:advertising:edit']"
            v-if="scope.row.status===1"
          >上架</el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status===1"
            v-hasPermi="['smy:advertising:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告平台" prop="platformType">
          <el-select v-model="form.platformType" placeholder="请选择广告平台">
            <el-option label="商城端" :value="0"></el-option>
            <el-option label="采购端" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告位置" prop="position">
          <el-select v-model="form.position" placeholder="请选择广告位置">
            <el-option
              v-for="item in positionList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加盟区域" v-if="form.position==10">
          <el-cascader
            size="large"
            :options="options"
            v-model="selectedOptions"
            @change="handleRegionChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="广告名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入广告名称" />
        </el-form-item>

        <el-form-item label="广告图">
          <image-upload v-model="form.image" :limit="1" />
        </el-form-item>

        <el-form-item label="排序值" prop="rank">
          <el-input v-model="form.rank" placeholder="请输入排序值" />
        </el-form-item>

        <!--        <el-form-item label="${comment}" prop="innerlinkid">-->
        <!--          <el-input v-model="form.innerlinkid" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="innerlinkkeyword">-->
        <!--          <el-input v-model="form.innerlinkkeyword" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="rank">-->
        <!--          <el-input v-model="form.rank" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="channelid">-->
        <!--          <el-input v-model="form.channelid" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="channellevel">-->
        <!--          <el-input v-model="form.channellevel" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="innerlinkname">-->
        <!--          <el-input v-model="form.innerlinkname" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="${comment}" prop="subjectid">-->
        <!--          <el-input v-model="form.subjectid" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdvertising, getAdvertising, delAdvertising, addAdvertising, updateAdvertising,changeAdvertisingStatus } from "@/api/smy/mallAdvertising";
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'

export default {
  name: "Advertising",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      advertisingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options: regionDataPlus,
      selectedOptions:[],
      positionList:[
        {value: 1, label: '首页海报图'},
        {value: 2, label: '首页通栏广告'},
        {value: 3, label: '首页小广告'},
        {value: 4, label: '成功支付广告'},
        {value: 5, label: '领券中心'},
        {value: 6, label: '秒杀活动'},
        {value: 7, label: '特价活动'},
        {value: 8, label: '预售活动'},
        {value: 9, label: '拼团活动'},
        {value: 10, label: '区域加盟商广告'},
      ],
      statusList:[
        {value: '1', label: '下架'},
        {value: '2', label: '上架'},
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createtime: null,
        image: null,
        innerlinkid: null,
        innerlinkkeyword: null,
        innerlinktype: null,
        name: null,
        position: null,
        rank: null,
        status: null,
        platformtype: null,
        channelid: null,
        channellevel: null,
        innerlinkname: null,
        subjectid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "广告名称不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "广告图不能为空", trigger: "change" }
        ],
        position: [
          { required: true, message: "广告位置不能为空", trigger: "blur" }
        ],
        rank: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        platformType: [
          { required: true, message: "广告发布平台不能为空", trigger: "change" }
        ],
        channelid: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        channellevel: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        subjectid: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listAdvertising(this.queryParams).then(response => {
        this.advertisingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleRegionChange(value){
      console.log(value);
      let provinceCode = value[0];
      let cityCode = value[1];
      let countyCode = value[2];
      if(provinceCode){
        this.form.provinceName = CodeToText[provinceCode];
      }
      if(cityCode){
        this.form.cityName = CodeToText[cityCode];
      }
      if(provinceCode && cityCode ===''){
        this.form.cityName = "全部";
        this.form.countyName = "全部";
      }
      if(countyCode){
        this.form.countyName = CodeToText[countyCode];
      }
      if(provinceCode && countyCode === ''){
        this.form.countyName = "全部";
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.selectedOptions=[];
      this.form = {
        id: null,
        createtime: null,
        image: null,
        innerlinkid: null,
        innerlinkkeyword: null,
        innerlinktype: null,
        name: null,
        position: null,
        rank: null,
        status: 0,
        platformtype: null,
        channelid: null,
        channellevel: null,
        innerlinkname: null,
        subjectid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdvertising(id).then(response => {
        this.form = response.data;
        let provinceCode='',cityCode='',countyCode='';
        if(response.data.provinceName){
          provinceCode = TextToCode[response.data.provinceName].code;
        }
        if(response.data.cityName && response.data.cityName !== '全部'){
          cityCode = TextToCode[response.data.provinceName][response.data.cityName].code;
        }
        this.selectedOptions=[provinceCode,cityCode];
        if(cityCode){
          if(response.data.countyName && response.data.countyName !== '全部'){
            countyCode = TextToCode[response.data.provinceName][response.data.cityName][response.data.countyName].code;
          }
          this.selectedOptions=[provinceCode,cityCode,countyCode];
        }

        this.open = true;
        this.title = "修改广告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        let image = this.form.image || [];
        if(image.length===0){
          this.$modal.msgError("广告图不能为空");
          return false;
        }
        this.form.image = image[0].name;
        if (valid) {
          if (this.form.id != null) {
            updateAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleChangeStatus(row,optType){
      const id = row.id;
      let message = "是否确认";
      if(optType === 'Y'){
        message += "上架";
      }
      if(optType === 'N'){
        message += "下架";
      }
      message+="名称为'"+row.name+"'的广告图";
      let optDto = {
        longId:id,
        optType
      }
      this.$modal.confirm(message).then(function() {
        return changeAdvertisingStatus(optDto);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function() {
        return delAdvertising(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/Advertising/export', {
        ...this.queryParams
      }, `Advertising_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
